package com.hdit.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.hdit.bean.Pager;
import com.hdit.bean.Pager.OrderType;
import com.hdit.dao.AuctionDao;
import com.hdit.entity.Auction;
import com.hdit.entity.Member;
import com.hdit.entity.Product;
import com.hdit.service.AuctionService;
import com.hdit.service.HtmlService;

import org.apache.commons.lang.StringUtils;
import org.compass.core.Compass;
import org.compass.core.CompassHits;
import org.compass.core.CompassQuery;
import org.compass.core.CompassQueryBuilder;
import org.compass.core.CompassSession;
import org.compass.core.CompassTemplate;
import org.compass.core.CompassQuery.SortDirection;
import org.compass.core.CompassQuery.SortPropertyType;
import org.compass.core.CompassQueryBuilder.CompassBooleanQueryBuilder;
import org.springframework.stereotype.Service;

/**
 * Service实现类 - 商品
 * ============================================================================
 */

@Service
public class AuctionServiceImpl extends BaseServiceImpl<Auction, String> implements AuctionService {

	@Resource
	private AuctionDao auctionDao;
	@Resource
	private CompassTemplate compassTemplate;
	@Resource
	private HtmlService htmlService;

	@Resource
	public void setBaseDao(AuctionDao auctionDao) {
		super.setBaseDao(auctionDao);
	}

	
	public List<Auction> getAuctionAuctionList(Auction auction) {
		return auctionDao.getAuctionList(auction);
	}
	
	
	public List<Auction> getAuctionList(int firstResult, int maxResults) {
		return auctionDao.getAuctionList(firstResult, maxResults);
	}
	
	
	public List<Auction> getBestAuctionList(int maxResults) {
		return auctionDao.getBestAuctionList(maxResults);
	}

	public List<Auction> getHotAuctionList(int maxResults) {
		return auctionDao.getHotAuctionList(maxResults);
	}


	public List<Auction> getNewAuctionList(int maxResults) {
		return auctionDao.getNewAuctionList(maxResults);
	}

	public List<Auction> getAuctionList(Date beginDate, Date endDate, int firstResult, int maxResults) {
		return auctionDao.getAuctionList(beginDate, endDate, firstResult, maxResults);
	}
	
	public Pager search(Pager pager) {
		Compass compass = compassTemplate.getCompass();
		CompassSession compassSession = compass.openSession();
		CompassQueryBuilder compassQueryBuilder = compassSession.queryBuilder();
		CompassBooleanQueryBuilder compassBooleanQueryBuilder = compassQueryBuilder.bool();

		CompassQuery compassQuery = compassBooleanQueryBuilder.addMust(compassQueryBuilder.term("isMarketable", true)).addMust(compassQueryBuilder.queryString("name:*" + pager.getKeyword() + "*").toQuery()).toQuery();
		
		if (StringUtils.isEmpty(pager.getOrderBy()) || pager.getOrderType() == null) {
			compassQuery.addSort("isBest", SortPropertyType.STRING, SortDirection.REVERSE)
			.addSort("isNew", SortPropertyType.STRING, SortDirection.REVERSE)
			.addSort("isHot", SortPropertyType.STRING, SortDirection.REVERSE);
		} else {
			if (pager.getOrderType() == OrderType.asc) {
				if(StringUtils.equalsIgnoreCase(pager.getOrderBy(), "price")) {
					compassQuery.addSort("price", SortPropertyType.FLOAT);
				}
			} else {
				if(StringUtils.equalsIgnoreCase(pager.getOrderBy(), "price")) {
					compassQuery.addSort("price", SortPropertyType.FLOAT, SortDirection.REVERSE);
				}
			}
		}
		
		CompassHits compassHits = compassQuery.hits();

		List<Auction> auctionList = new ArrayList<Auction>();

		int firstResult = (pager.getPageNumber() - 1) * pager.getPageSize();
		int maxReasults = pager.getPageSize();
		int totalCount = compassHits.length();

		int end = Math.min(totalCount, firstResult + maxReasults);
		for (int i = firstResult; i < end; i++) {
			Auction auction = (Auction) compassHits.data(i);
			auctionList.add(auction);
		}
		compassSession.close();
		pager.setList(auctionList);
		pager.setTotalCount(totalCount);
		return pager;
	}
	
	public Pager getFavoriteAuctionPager(Member member, Pager pager) {
		return auctionDao.getFavoriteAuctionPager(member, pager);
	}
	
	public Long getStoreAlertCount() {
		return auctionDao.getStoreAlertCount();
	}
	
	public Long getMarketableAuctionCount() {
		return auctionDao.getMarketableAuctionCount();
	}
	
	public Long getUnMarketableAuctionCount() {
		return auctionDao.getUnMarketableAuctionCount();
	}
	
	// 重写方法，删除对象的同时删除已生成的HTML静态文件、商品图片文件
	@Override
	public void delete(Auction auction) {
		auctionDao.delete(auction);
	}

	// 重写方法，删除对象的同时删除已生成的HTML静态文件、商品图片文件
	@Override
	public void delete(String id) {
		Auction auction = auctionDao.load(id);
		this.delete(auction);
	}

	// 重写方法，删除对象的同时删除已生成的HTML静态文件、商品图片文件
	@Override
	public void delete(String[] ids) {
		for (String id : ids) {
			this.delete(id);
		}
	}

	// 重写方法，保存对象的同时处理价格精度并生成HTML静态文件
	@Override
	public String save(Auction auction) {
	
		String id = auctionDao.save(auction);
		return id;
	}

	// 重写方法，更新对象的同时处理价格精度并重新生成HTML静态文件
	@Override
	public void update(Auction auction) {
		String id = auction.getId();
		
		auctionDao.update(auction);
	}


	@Override
	public Auction getMaxAuctionPrice(String productid) {
		
		return auctionDao.getMaxAuctionPrice(productid);
	}
	
	

}